<?php require 'common/head.php'; ?>
<?php
    require '../db_connection.php';

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $file  = $_FILES['file'];
        if ($file['tmp_name']) {
          $file_name = 'uploads/file/' . time() . '_' . $file['name'];
          $path  = "../" . $file_name;
          move_uploaded_file($file['tmp_name'], "../" . $file_name);
        } else {
          $file_name = $_POST['file_path'];
        }
        $resource_name = $_POST['resource_name'];
        $type = $_POST['type'];
        $description = $_POST['description'];
        $upload_time = date('Y-m-d H:i:s');
        $id = $_POST['id'];
        $sql = "UPDATE `educational_resources` SET `resource_name` = '$resource_name', `type` = '$type',
         `description` = '$description', `upload_time` = '$upload_time', `file_path` = '$file_name' WHERE id = $id";
        $result = $conn->query($sql);
        if ($result) {
          die("<script>alert('编辑成功');location.href='resource_list.php';</script>");
        } else {
          die("<script>alert('编辑失败');history.back();</script>");
        }
    }

     $id   = $_GET['id'];
     $sql    = "SELECT * FROM educational_resources WHERE id=$id";
     $result = $conn->query($sql);
     $info   = $result->fetch_assoc();
 
     $conn->close();
?>
</head>
<body>
<div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>资源编辑</strong></div>
  <div class="body-content">
  <form method="post" class="form-x" action="resource_edit.php" style="margin-top:20px;" enctype="multipart/form-data"> 
      <div class="form-group">
        <div class="label">
          <label>资源名称</label>
        </div>
        <div class="field">
          <input type="text" class="input" value="<?php echo $info['resource_name']; ?>" name="resource_name" data-validate="required:请输入资源名称" />
          <input type="hidden" name="id" value="<?php echo $info['id']; ?>" />
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>类型</label>
        </div>
        <div class="field">
          <input type="text" class="input" value="<?php echo $info['type']; ?>" name="type" data-validate="required:请输入资源类型" />
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>上传文件</label>
        </div>
        <div class="field">
            <input type="file" name="file"  id="file-apply" class="input tips" style="width:25%; float:left;"  value=""  data-toggle="hover" data-place="right" data-validate="required:请上传表格"  />
            <input type="hidden" name="file_path" value="<?php echo $info['file_path']; ?>" />
          </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>描述</label>
        </div>
        <div class="field">
          <textarea class="input" name="description" style="height:90px;"><?php echo $info['description']; ?></textarea>
        </div>
      </div> 
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <button class="button bg-main" type="submit">编辑</button>
        </div>
      </div>    
    </form>
  </div>
</div>

</body></html>   